import utils from '@/utils'
import { fetchList } from '@/api/category'
const category = {
  state: {
    categoryList: [],
    categoryTotal: 0
  },
  getters: {
    categoryList: state => state.categoryList, 
    categoryTotal:  state => state.categoryTotal
  },
  mutations: {
    SET_CATEGORYLIST: (state, categoryList) => {
      state.categoryList = categoryList
    },
    SET_CATEGORYTOTAL: (state, total) => {
      state.categoryTotal = total
    }
  },
  actions: {
    async getCategoryList ({commit}, listQuery) {
      let res = await fetchList(listQuery)
      let items = res.data.items
      let total = res.data.total
      console.log("items:",items)
      items.map(item => {
        item.category_labels = []
        item.time = new Date(item.created_at).getTime()
      })
      items.map((item, index) => {
        item.category_serial_number = index + 1
        delete item.time
      })
      commit('SET_CATEGORYLIST', items)
      commit('SET_CATEGORYTOTAL', total)
      return items
    }
  }
}
export default category
